// VBConversions Note: VB project level imports
using System.Diagnostics;
using System;
using System.Xml.Linq;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using Microsoft.VisualBasic;
using System.Data;
using System.Collections.Generic;
using Microsoft.VisualBasic.Compatibility;
using System.Linq;
// End of VB project level imports

using System.IO;

namespace 喷码数据生成工具
{
	public partial class frm_RemedyData
	{
		public frm_RemedyData()
		{
			InitializeComponent();
		}
		DataSet ds = new DataSet("datasample");
		public struct OldDataStructure
		{
			public ArrayList Data;
		}
		public struct SNumber
		{
			public string StartNo;
			public string EndNo;
		}
		public struct SearchData
		{
			public List<SNumber> SearchNumber;
			public int CheckLength;
			public string Suffix;
			public string Prefix;
		}
		public enum EventAction
		{
			None = 0,
			READOLDDATA = 1,
			READFINDNUMBER = 2,
			WRITEFINDRESULT = 3
		}
		public struct ParamText
		{
			public string Value;
			public int Index;
		}
		public long CurrentValue;
		List<OldDataStructure> ReadData = new List<OldDataStructure>();
		private delegate void InvokeChangeProgess(long Value);
		private delegate void InvokeEnd(bool IsError, string ErrText);
		private delegate void InvokeEndRead();
		private delegate void InvokeShowSample(ParamText Parame);
		private const int MaxHeight = 406;
		private const int MinHeight = 380;
		System.Threading.Thread th;
		System.Threading.Thread fh;
		private EventAction EventState = EventAction.None;
		private int ProgressIndex;
		private string ProgressText = "....................";
		int OldDataSelectedIndex = 0;
		long FiledSize = 0;
		public void Btn_ReadData_Enter(System.Object sender, System.EventArgs e)
		{
			
		}
		
		public void frm_RemedyData_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
		{
			e.Cancel = true;
			if (EventState != EventAction.None)
			{
				if (MessageBox.Show(this, "有操作未完成是否继续退出？", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
				{
				}
			}
			return ;
//			e.Cancel = false;
		}
		
		public void frm_RemedyData_Load(object sender, System.EventArgs e)
		{
			CommFunction.ChangeButtonFromFrm(this.GroupBox1);
			CommFunction.ChangeButtonFromFrm(this.GroupBox2);
			CommFunction.ChangeButtonColor(Btn_OrderNumber);
			LV_FindData.Columns[0].Width = (int) ((double) LV_FindData.Width / 2 - 4);
			LV_FindData.Columns[1].Width = (int) ((double) LV_FindData.Width / 2 - 2);
			LV_DataSample.Columns[0].Width = 50;
			LV_DataSample.Columns[1].Width = LV_DataSample.Width - LV_DataSample.Columns[0].Width - 4;
			this.Height = MinHeight;
			// NeroBar1.GlowMode = NeroBar.NeroBarGlowModes.None
			ViewDataMenu.Renderer = CommFunction.MenuSkinRender;
		}
		
		public void TB_Lines_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Strings.Asc(e.KeyChar) == 8)
			{
				return ;
			}
			if (Strings.Asc(e.KeyChar) < 48 || Strings.Asc(e.KeyChar) > 57)
			{
				e.Handled = true;
			}
		}
		
		public void TB_DataPath_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			e.Handled = true;
		}
		
		public void TB_FindDataPath_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			e.Handled = true;
		}
		
		public void TB_CheckLenght_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if (Strings.Asc(e.KeyChar) == 8)
			{
				return ;
			}
			if (Strings.Asc(e.KeyChar) < 48 || Strings.Asc(e.KeyChar) > 57)
			{
				e.Handled = true;
			}
		}
		
		public void TB_CheckLenght_LostFocus(object sender, System.EventArgs e)
		{
			if (Conversion.Val(TB_CheckLenght.Text) < 0)
			{
				Interaction.MsgBox("校验码长度不能小于0！", MsgBoxStyle.Critical, "错误");
			}
			TB_CheckLenght.Text = System.Convert.ToString(0);
		}
		
		public void TB_Lines_LostFocus(object sender, System.EventArgs e)
		{
			if (Conversion.Val(TB_Lines.Text) < 1)
			{
				Interaction.MsgBox("校验码长度不能小于0！", MsgBoxStyle.Critical, "错误");
			}
			TB_Lines.Text = System.Convert.ToString(1);
		}
		
		public void TB_Lines_TextChanged(System.Object sender, System.EventArgs e)
		{
			if (ReadData.Count <= 0)
			{
				LV_DataSample.Items.Clear();
				for (var i = 0; i <= Conversion.Val(TB_Lines.Text) - 1; i++)
				{
					LV_DataSample.Items.Add("Var" + (i + 1));
				}
				string FileName = TB_DataPath.Text;
				int sCount = 0;
				string TmpValue = null;
				if (File.Exists(FileName))
				{
					FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
					StreamReader fw = new StreamReader(fs, System.Text.Encoding.Default);
					while (!(sCount >= Conversion.Val(TB_Lines.Text)))
					{
						string ReadTmp = fw.ReadLine();
						ListViewItem.ListViewSubItem tmpItem = default(ListViewItem.ListViewSubItem);
						try
						{
							tmpItem = LV_DataSample.Items[sCount].SubItems[1];
						}
						catch
						{
							tmpItem = null;
						}
						if (tmpItem != null)
						{
							tmpItem.Text = ReadTmp;
						}
						else
						{
							LV_DataSample.Items[sCount].SubItems.Add(ReadTmp);
						}
						if (string.IsNullOrEmpty(TmpValue))
						{
							TmpValue = ReadTmp;
						}
						else
						{
							TmpValue = TmpValue + "\r\n" + ReadTmp;
						}
						sCount++;
					}
					fw.Close();
					fs.Close();
					//  If Not String.IsNullOrEmpty(TmpValue) Then TmpValue += vbCrLf : FiledSize = System.Text.Encoding.Default.GetByteCount(TmpValue)
				}
			}
		}
		
		public void Btn_OpenOldData_Click(System.Object sender, System.EventArgs e)
		{
			FileDialog.Title = "打开源数据文件";
			FileDialog.Filter = "文本文件(*.txt)|*.txt";
			FileDialog.FileName = "1.txt";
			FileDialog.CheckFileExists = true;
			if (FileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				TB_DataPath.Text = FileDialog.FileName;
				TB_DataPath.SelectionStart = FileDialog.FileName.Length;
				string tmp = RemedyCode.ShowInput(this, "每组行数", System.Convert.ToString(1), true);
				if (Conversion.Val(tmp) > 1)
				{
					this.TB_Lines.Text = tmp;
				}
				else
				{
					TB_Lines_TextChanged(sender, e);
				}
				ReadData.Clear();
				Btn_StartFindData.Enabled = false;
				
			}
		}
		
		public void Btn_ReadOldData_Click(System.Object sender, System.EventArgs e)
		{
			if (File.Exists(TB_DataPath.Text) == false)
			{
				MessageBox.Show(this, "数据文件不存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return ;
//			EventState = EventAction.READOLDDATA;
//			Timer1.Interval = 1000;
//			ProgressIndex = 0;
//			Timer1.Enabled = true;
//			Btn_OpenOldData.Enabled = false;
//			Btn_ReadOldData.Enabled = false;
//			OldDataSelectedIndex = 0;
//			th = new System.Threading.Thread(new System.Threading.ThreadStart(ReadOldData));
//			th.Start();
		}
		private void ReadOldData()
		{
			if (File.Exists(TB_DataPath.Text))
			{
				int sIndex = 0;
				int sCount = 0;
				bool hasError = false;
				string ErrTxt = "";
				sCount = (int) (Conversion.Val(TB_Lines.Text));
				OldDataStructure tmpData = new OldDataStructure();
				bool IsRead = false;
				ReadData = new List<OldDataStructure>();
				FileStream fs = new FileStream(TB_DataPath.Text, FileMode.Open, FileAccess.Read);
				if (FiledSize <= 0)
				{
					StreamReader fw = new StreamReader(fs, System.Text.Encoding.Default);
					try
					{
						while (!fw.EndOfStream)
						{
							string tmp = fw.ReadLine();
							sIndex++;
							if (sCount > 1)
							{
								if (sIndex > 1 & sIndex < sCount)
								{
									goto AddOnlyData;
								}
								if (sCount == 1 & sIndex == 1)
								{
									goto AddData;
								}
								if (sIndex == 1)
								{
									tmpData = new OldDataStructure();
									tmpData.Data = new ArrayList();
									tmpData.Data.Add(tmp);
								}
								else if (sIndex == sCount)
								{
AddData:
									if (tmpData.Data == null)
									{
										tmpData = new OldDataStructure();
									}
									tmpData.Data = new ArrayList();
									tmpData.Data.Add(tmp);
									ReadData.Add(tmpData);
									if (IsRead == false)
									{
										for (var i = 0; i <= tmpData.Data.Count - 1; i++)
										{
											ParamText TmpP = new ParamText();
											TmpP.Value = System.Convert.ToString(tmpData.Data[i]);
											TmpP.Index = System.Convert.ToInt32(i);
											this.Invoke(new InvokeShowSample(ShowSampleData), TmpP);
										}
										IsRead = true;
									}
									sIndex = 0;
								}
								else if (sIndex == (sIndex > 1 & sIndex < sCount))
								{
AddOnlyData:
									tmpData.Data.Add(tmp);
									// Me.Text = tmp
								}
							}
							else
							{
								tmpData = new OldDataStructure();
								tmpData.Data = new ArrayList();
								tmpData.Data.Add(tmp);
								ReadData.Add(tmpData);
								if (IsRead == false)
								{
									ParamText TmpP = new ParamText();
									TmpP.Value = tmp;
									TmpP.Index = 0;
									this.Invoke(new InvokeShowSample(ShowSampleData), TmpP);
									IsRead = true;
								}
							}
							// My.Application.DoEvents()
						}
						
						//    MsgBox(ReadData(0).Data.Item(2))
					}
					catch (Exception ex)
					{
						hasError = true;
						ErrTxt = ex.Message;
					}
					fw.Close();
					fs.Close();
				}
				else
				{
					while (!(fs.Position >= fs.Length))
					{
						string tmpValue = "";
						byte[] tmpByte = null;
						string[] a = null;
						tmpByte = new byte[(int) (FiledSize - 1) + 1];
						fs.Read(tmpByte, 0, FiledSize);
						tmpValue = System.Text.Encoding.Default.GetString(tmpByte);
						tmpData = new OldDataStructure();
						tmpData.Data = new ArrayList();
						a = tmpValue.Split("\r\n");
						for (var i = 0; i <= a.Length - 1; i++)
						{
							tmpData.Data.Add(a[(int) i]);
						}
						ReadData.Add(tmpData);
					}
				}
				if (!hasError)
				{
					MessageBox.Show("读取文件结束，共有：" + System.Convert.ToString(ReadData.Count) + "项数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
				}
				else
				{
					MessageBox.Show(ErrTxt, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				}
				this.Invoke(new InvokeEndRead(EndReadOldData));
				try
				{
					fh.Abort();
				}
				catch (Exception)
				{
				}
			}
		}
		private void ShowSampleData(ParamText parame)
		{
			int sIndex = parame.Index;
			ListViewItem litem = LV_DataSample.Items[parame.Index];
			ListViewItem.ListViewSubItem subItem;
			try
			{
				subItem = litem.SubItems[1];
				subItem.Text = parame.Value;
			}
			catch
			{
				litem.SubItems.Add(parame.Value);
				return ;
			}
		}
		private void EndReadOldData()
		{
			Timer1.Enabled = false;
			this.Height = MinHeight;
			this.Text = "提取号码";
			EventState = EventAction.None;
			Btn_OpenOldData.Enabled = true;
			Btn_ReadOldData.Enabled = true;
			Btn_StartFindData.Enabled = true;
			if (ReadData.Count > 0)
			{
				Btn_OrderNumber.Enabled = true;
			}
		}
		public void Btn_ReadFindData_Click(System.Object sender, System.EventArgs e)
		{
			if (File.Exists(TB_FindDataPath.Text))
			{
				LV_FindData.Items.Clear();
				FileStream fs = new FileStream(TB_FindDataPath.Text, FileMode.Open, FileAccess.Read);
				StreamReader fw = new StreamReader(fs, System.Text.Encoding.Default);
				while (!fw.EndOfStream)
				{
					string tmp = fw.ReadLine();
					if (tmp != "")
					{
						int a = 0;
						string[] R = null;
						a = tmp.IndexOf(",");
						if (a > 0)
						{
							R = tmp.Split(',');
							if (Information.IsNumeric(R[0]) && Information.IsNumeric(R[1]))
							{
								ListViewItem TmpItem = new ListViewItem();
								TmpItem.Text = R[0];
								TmpItem.SubItems.Add(R[1]);
								LV_FindData.Items.Add(TmpItem);
							}
						}
						else
						{
							if (Information.IsNumeric(tmp))
							{
								ListViewItem tmpItem = new ListViewItem();
								tmpItem.Text = tmp;
								tmpItem.SubItems.Add(tmp);
								LV_FindData.Items.Add(tmpItem);
							}
						}
					}
				}
				fw.Close();
				fs.Close();
			}
		}
		
		public void Btn_OpenFindDataPath_Click(System.Object sender, System.EventArgs e)
		{
			FileDialog.Title = "打开要取号码信息文本";
			FileDialog.FileName = "1.txt";
			FileDialog.CheckFileExists = true;
			FileDialog.Filter = "文本文件(*.txt)|*.txt";
			if (FileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
			{
				TB_FindDataPath.Text = FileDialog.FileName;
				TB_FindDataPath.SelectionStart = FileDialog.FileName.Length;
			}
		}
		
		public void Btn_DelData_Click(System.Object sender, System.EventArgs e)
		{
			foreach (ListViewItem item in LV_FindData.Items)
			{
				if (item.Selected)
				{
					LV_FindData.Items.Remove(item);
				}
			}
		}
		
		public void Btn_AddData_Click(System.Object sender, System.EventArgs e)
		{
			if (string.IsNullOrEmpty(TB_StartNo.Text) && string.IsNullOrEmpty(TB_EndNo.Text))
			{
				Interaction.MsgBox("起始号码和终止号码不能为空！", MsgBoxStyle.Information, "错误");
				return ;
			}
			if (!(Information.IsNumeric(TB_StartNo.Text)) || !(Information.IsNumeric(TB_EndNo.Text)))
			{
				Interaction.MsgBox("起始号码和终止号码必须为数值型数据！", MsgBoxStyle.Critical, "错误");
				return ;
			}
			if (Conversion.Val(TB_StartNo.Text) > Conversion.Val(TB_EndNo.Text))
			{
				MessageBox.Show(this, "起始号码不能大于终止号码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			return ;
//			ListViewItem tmpItem = new ListViewItem();
//			tmpItem.Text = TB_StartNo.Text;
//			tmpItem.SubItems.Add(TB_EndNo.Text);
//			LV_FindData.Items.Add(tmpItem);
		}
		private string getOnlyNumber(string Value, string prefix, string suffix, int CheckLength)
		{
			string Result = Value;
			if (!(string.IsNullOrEmpty(prefix)))
			{
				Result = Result.Substring(prefix.Length);
			}
			if (!(string.IsNullOrEmpty(suffix)))
			{
				Result = Result.Substring(0, Result.Length - suffix.Length);
			}
			if (CheckLength >= 1)
			{
				Result = Result.Substring(0, Result.Length - CheckLength);
			}
			return Result;
		}
		
		public void Btn_StartFindData_Click(System.Object sender, System.EventArgs e)
		{
			this.Height = MaxHeight;
			if (ReadData.Count <= 0)
			{
				MessageBox.Show(this, "未读取源数据，请先读取源文本数据再进行查找操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
			}
			return ;
//			if (LV_FindData.Items.Count <= 0)
//			{
//				MessageBox.Show(this, "未设置要查找的号码段，不能进行查找操作！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//				}
//				return ;
//				SearchData SearchInfo = new SearchData();
//				SearchInfo.CheckLength = (int) (Conversion.Val(TB_CheckLenght.Text));
//				SearchInfo.Prefix = TB_Prefix.Text;
//				SearchInfo.Suffix = TB_Suffix.Text;
//				List<SNumber> Data = new List<SNumber>();
//				for (var i = 0; i <= LV_FindData.Items.Count - 1; i++)
//				{
//					SNumber TmpItem = new SNumber();
//					TmpItem.StartNo = LV_FindData.Items[i].Text;
//					TmpItem.EndNo = LV_FindData.Items[i].SubItems[1].Text;
//					Data.Add(TmpItem);
//					}
//					SearchInfo.SearchNumber = Data;
//					SetObjectEnabled(GroupBox1, false);
//					SetObjectEnabled(GroupBox2, false);
//					SetObjectEnabled(GroupBox3, false);
//					NeroBar1.Maximum = ReadData.Count;
//					EventState = EventAction.WRITEFINDRESULT;
//					Timer1.Interval = 1;
//					Timer1.Enabled = true;
//					string SaveFile = TB_SaveFileName.Text;
//					if (File.Exists(SaveFile))
//					{
//						TB_SaveFileName.Text = CommFunction.getAddFileName(SaveFile);
//						}
//						TB_SaveFileName.SelectionStart = TB_SaveFileName.Text.Length;
//						th = new System.Threading.Thread(new System.Threading.ThreadStart(StartFindData));
//						th.Start(SearchInfo);
					}
					private void StartFindData(SearchData Param)
					{
						string CaculateStr = "";
						long TmpValue = 0;
						int LSHP = (int) (Conversion.Val(TB_Position.Text) - 1);
						FileStream fs = new FileStream(TB_SaveFileName.Text, FileMode.Create, FileAccess.Write);
						StreamWriter fw = new StreamWriter(fs, System.Text.Encoding.Default);
						try
						{
							for (var i = 0; i <= ReadData.Count - 1; i++)
							{
								bool ExistB = false;
								CaculateStr = ReadData[System.Convert.ToInt32(i)].Data.Item(LSHP).ToString();
								TmpValue = (long) (Conversion.Val(getOnlyNumber(CaculateStr, Param.Prefix, Param.Suffix, Param.CheckLength)));
								for (var j = 0; j <= Param.SearchNumber.Count - 1; j++)
								{
									if (TmpValue >= Conversion.Val(Param.SearchNumber[System.Convert.ToInt32(j)].StartNo) && TmpValue <= Conversion.Val(Param.SearchNumber[j].EndNo))
									{
										ExistB = true;
										break;
									}
								}
								if (ExistB)
								{
									for (var z = 0; z <= ReadData[System.Convert.ToInt32(i)].Data.Count - 1; z++)
									{
										fw.WriteLine(ReadData[System.Convert.ToInt32(i)].Data.Item(z).ToString());
									}
								}
								// Me.Invoke(New InvokeChangeProgess(AddressOf ChangeProgress), i + 1)
								CurrentValue = System.Convert.ToInt64(i + 1);
							}
							fw.Close();
							fs.Close();
							try
							{
								th.Abort();
								this.Invoke(new InvokeEnd(EndFind), false, "");
							}
							catch
							{
							}
						}
						catch (Exception ex)
						{
							fw.Close();
							fs.Close();
							if (ex.Message != "正在中止线程。")
							{
								this.Invoke(new InvokeEnd(EndFind), true, ex.Message);
							}
							else
							{
								this.Invoke(new InvokeEnd(EndFind), false, "");
							}
							try
							{
								th.Abort();
							}
							catch
							{
							}
						}
					}
					private void ChangeProgress(long Value)
					{
						NeroBar1.Value = Value;
					}
					private void EndFind(bool IsErr, string ErrText)
					{
						if (IsErr == false)
						{
							MessageBox.Show(this, "数据操作结束！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
						}
						else
						{
							MessageBox.Show(this, ErrText, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
						SetObjectEnabled(GroupBox1, true);
						SetObjectEnabled(GroupBox2, true);
						SetObjectEnabled(GroupBox3, true);
						this.Height = MinHeight;
						Timer1.Enabled = false;
						NeroBar1.Value = 0;
						EventState = EventAction.None;
					}
					public void TB_DataPath_TextChanged(System.Object sender, System.EventArgs e)
					{
						this.TB_SaveFileName.Text = CommFunction.getAddFileName(TB_DataPath.Text);
						TB_SaveFileName.SelectionStart = TB_SaveFileName.Text.Length;
					}
					
					public void TB_StartNo_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
					{
						if (Strings.Asc(e.KeyChar) == 8)
						{
							return ;
						}
						if (Strings.Asc(e.KeyChar) < 48 || Strings.Asc(e.KeyChar) > 57)
						{
							e.Handled = true;
						}
					}
					
					public void TB_EndNo_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
					{
						if (Strings.Asc(e.KeyChar) == 8)
						{
							return ;
						}
						if (Strings.Asc(e.KeyChar) < 48 || Strings.Asc(e.KeyChar) > 57)
						{
							e.Handled = true;
						}
					}
					private void SetObjectEnabled(Control SearchParent, bool Value)
					{
						if (SearchParent.Controls.Count > 0)
						{
							foreach (Control item in SearchParent.Controls)
							{
								item.Enabled = Value;
							}
						}
					}
					
					public void Timer1_Tick(System.Object sender, System.EventArgs e)
					{
						if (EventState == EventAction.WRITEFINDRESULT)
						{
							NeroBar1.Value = CurrentValue;
						}
						else if (EventState == EventAction.READOLDDATA)
						{
							ProgressIndex++;
							if (ProgressIndex > 10)
							{
								ProgressIndex = 1;
							}
							this.Text = "读取数据" + ProgressText.Substring(0, ProgressIndex);
						}
					}
					
					public void frm_RemedyData_Resize(object sender, System.EventArgs e)
					{
						
					}
					
					public void LV_DataSample_Click(System.Object sender, System.EventArgs e)
					{
						
					}
					
					public void LV_DataSample_MouseUp(System.Object sender, System.Windows.Forms.MouseEventArgs e)
					{
						if (e.Button == System.Windows.Forms.MouseButtons.Right)
						{
							if (ReadData.Count <= 0)
							{
								foreach (ToolStripItem item in ViewDataMenu.Items)
								{
									item.Enabled = false;
								}
							}
							else
							{
								foreach (ToolStripItem item in ViewDataMenu.Items)
								{
									item.Enabled = true;
								}
								if (OldDataSelectedIndex == 0)
								{
									Menu_Back.Enabled = false;
								}
								Menu_First.Enabled = false;
								if (OldDataSelectedIndex == ReadData.Count - 1)
								{
									Menu_Next.Enabled = false;
								}
								Menu_Last.Enabled = false;
							}
						}
					}
					private void ShowText(long Index)
					{
						if (Index < 0 | Index >= ReadData.Count)
						{
							return ;
						}
						OldDataStructure tmpData = new OldDataStructure();
						tmpData = ReadData[Index];
						for (var i = 0; i <= tmpData.Data.Count - 1; i++)
						{
							ListViewItem.ListViewSubItem tmpItem;
							try
							{
								tmpItem = LV_DataSample.Items[i].SubItems[1];
								tmpItem.Text = System.Convert.ToString(tmpData.Data[i]);
							}
							catch (Exception)
							{
								try
								{
									tmpItem = LV_DataSample.Items[i].SubItems.Add(tmpData.Data[i]);
								}
								catch
								{
								}
							}
						}
					}
					
					public void Menu_Back_Click(System.Object sender, System.EventArgs e)
					{
						if (OldDataSelectedIndex - 1 >= 0)
						{
							OldDataSelectedIndex--;
							ShowText(OldDataSelectedIndex);
						}
					}
					
					public void Menu_Next_Click(System.Object sender, System.EventArgs e)
					{
						if (OldDataSelectedIndex + 1 < ReadData.Count)
						{
							OldDataSelectedIndex++;
							ShowText(OldDataSelectedIndex);
						}
					}
					
					public void Menu_First_Click(System.Object sender, System.EventArgs e)
					{
						OldDataSelectedIndex = 0;
						ShowText(OldDataSelectedIndex);
					}
					
					public void Menu_Last_Click(System.Object sender, System.EventArgs e)
					{
						OldDataSelectedIndex = ReadData.Count - 1;
						ShowText(OldDataSelectedIndex);
					}
					
					public void Menu_Goto_Click(System.Object sender, System.EventArgs e)
					{
						string tmp = RemedyCode.ShowInput(this, "InputBox", "", true);
						long a = (long) (Conversion.Val(tmp) - 1);
						if (a != OldDataSelectedIndex & a >= 0 & a < ReadData.Count)
						{
							OldDataSelectedIndex = a;
							ShowText(OldDataSelectedIndex);
						}
					}
					
					public void TB_StartNo_Enter(System.Object sender, System.EventArgs e)
					{
						TB_StartNo.SelectionStart = 0;
						TB_StartNo.SelectionLength = TB_StartNo.Text.Length;
					}
					
					public void TB_EndNo_Enter(System.Object sender, System.EventArgs e)
					{
						TB_EndNo.SelectionStart = 0;
						TB_EndNo.SelectionLength = TB_EndNo.Text.Length;
					}
					
					public void TB_Position_KeyPress(System.Object sender, System.Windows.Forms.KeyPressEventArgs e)
					{
						if (Strings.Asc(e.KeyChar) == 8)
						{
							return ;
						}
						if (Strings.Asc(e.KeyChar) <= 48 || Strings.Asc(e.KeyChar) > Strings.Asc(TB_Lines.Text))
						{
							e.Handled = true;
						}
						string tmp = TB_Position.Text + System.Convert.ToString(e.KeyChar);
						if (Conversion.Val(tmp) > Conversion.Val(TB_Lines.Text))
						{
							e.Handled = true;
						}
					}
					
					public void TB_Position_LostFocus(object sender, System.EventArgs e)
					{
						if (string.IsNullOrEmpty(TB_Position.Text))
						{
							TB_Position.Text = "1";
						}
						return ;
						
					}
					
					public void TB_Position_TextChanged(object sender, System.EventArgs e)
					{
						
					}
					
					public void Btn_OrderNumber_Click(System.Object sender, System.EventArgs e)
					{
						if (ReadData.Count <= 0)
						{
							MessageBox.Show(this, "未读入任何数据，请先读取源数据！", "提示");
						}
						Btn_OrderNumber.Enabled = false;
						if (ReadData.Count % 2 != 0)
						{
							MessageBox.Show(this, "数据不是偶数项，不能进行转换！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
						}
						return ;
//						if (RadioButton1.Checked)
//						{
//							NeroBar1.Maximum = ReadData.Count;
//							}
//							else
//							{
//								NeroBar1.Maximum = (double) ReadData.Count / 2;
//								}
//								Timer1.Interval = 1;
//								Timer1.Enabled = true;
//								this.Height = MaxHeight;
//								SetObjectEnabled(GroupBox1, false);
//								SetObjectEnabled(GroupBox2, false);
//								SetObjectEnabled(GroupBox3, false);
//								EventState = EventAction.WRITEFINDRESULT;
//								System.Threading.Thread gth = default(System.Threading.Thread);
//								gth = new System.Threading.Thread(new System.Threading.ThreadStart(StartSort));
//								gth.Start();
							}
							private void StartSort()
							{
								try
								{
									bool ZX = true;
									int LSHP = (int) (Conversion.Val(TB_Position.Text) - 1);
									List<OldDataStructure> Result1 = new List<OldDataStructure>();
									List<OldDataStructure> Result2 = new List<OldDataStructure>();
									List<OldDataStructure> Result = new List<OldDataStructure>();
									ZX = RadioButton1.Checked;
									if (ZX)
									{
										for (var i = 0; i <= ReadData.Count - 1; i++)
										{
											if (i % 2 == 0)
											{
												Result1.Add(ReadData[System.Convert.ToInt32(i)]);
											}
											else
											{
												Result2.Add(ReadData[System.Convert.ToInt32(i)]);
											}
											CurrentValue++;
										}
										Result.AddRange(Result1);
										Result.AddRange(Result2);
										ReadData = Result;
									}
									else
									{
										long Count = (long) ((double) ReadData.Count / 2);
										for (var i = 0; i <= Count - 1; i++)
										{
											Result.Add(ReadData[System.Convert.ToInt32(i)]);
											Result.Add(ReadData[System.Convert.ToInt32(i + Count)]);
											CurrentValue++;
										}
										ReadData = Result;
									}
									try
									{
										System.Threading.Thread tth = System.Threading.Thread.CurrentThread;
										tth.Abort();
									}
									catch
									{
										
									}
								}
								catch (Exception ex)
								{
									if (ex.Message != "正在中止线程。")
									{
										this.Invoke(new InvokeEnd(EndFind), true, ex.Message);
									}
									else
									{
										this.Invoke(new InvokeEnd(EndFind), false, "");
									}
									try
									{
										th.Abort();
									}
									catch
									{
									}
								}
							}
							
							public void 导出数据ToolStripMenuItem_Click(System.Object sender, System.EventArgs e)
							{
								SaveFileDialog Dialog = new SaveFileDialog();
								if (ReadData.Count <= 0)
								{
									MessageBox.Show("没有找到可保存数据！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
								}
								return ;
//								Dialog.Title = "保存";
//								Dialog.Filter = "文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
//								Dialog.CheckPathExists = true;
//								Dialog.FileName = "1";
//								if (Dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
//								{
//									if (File.Exists(Dialog.FileName))
//									{
//										if (MessageBox.Show(this, "文件已经存在是否覆盖？", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.No)
//										{
//											return ;
//											}
//											}
//											FileStream fs = new FileStream(Dialog.FileName, FileMode.Create, FileAccess.Write);
//											StreamWriter fw = new StreamWriter(fs, System.Text.Encoding.Default);
//											for (var i = 0; i <= ReadData.Count - 1; i++)
//											{
//												for (var j = 0; j <= ReadData[System.Convert.ToInt32(i)].Data.Count - 1; j++)
//												{
//													fw.WriteLine(ReadData[System.Convert.ToInt32(i)].Data.Item(j).ToString());
//													}
//													}
//													fw.Close();
//													fs.Close();
//													MessageBox.Show(this, "数据保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
//													}
												}
											}
										}
